import Vue from 'vue'
import App from './App'

require('./style/common.less'); //引入全局的base文件

import 'vue2-animate/dist/vue2-animate.min.css';
import router from './router'

Vue.config.productionTip = false

import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'

Vue.use(MintUI);

import { Actionsheet } from 'mint-ui';

Vue.component(Actionsheet.name, Actionsheet);
import { DatetimePicker } from 'mint-ui';

Vue.component(DatetimePicker.name, DatetimePicker);
import { Picker } from 'mint-ui';

Vue.component(Picker.name, Picker);
import { Popup } from 'mint-ui';

Vue.component(Popup.name, Popup);
import { Swipe, SwipeItem } from 'mint-ui';

Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);

import VueAwesomeSwiper from 'vue-awesome-swiper'
Vue.use(VueAwesomeSwiper)

//图片缩放
import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'

let options = {
    fullscreenEl: false, //关闭全屏按钮
    maxSpreadZoom: 1.5,//手势时的最大缩放级别
    pinchToClose: true,//画廊的背景将逐渐淡出。手势完成后，图库将关闭
    closeOnVerticalDrag: true,//垂直拖动时以及图像未缩放时关闭图库。
    mouseUsed: false,//键盘左或右箭头键导航。
    closeEl: false,//关闭按钮
    tapToClose: true,//点击大图关闭
    zoomEl: false,//缩放按钮
}
Vue.use(preview, options);

import axios from 'axios'
import Qs from 'qs'


//控制axios简单请求
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.defaults.transformRequest = [obj => Qs.stringify(obj)]
Vue.prototype.axios = axios;
Vue.prototype.qs = Qs;

Vue.filter('money', function (val) {
    if (!val) return 0
    val = val.toString().replace(/\$|,/g, '');
    if (isNaN(val)) {
        val = "0";
    }
    let sign = (val == (val = Math.abs(val)));
    val = Math.floor(val * 100 + 0.50000000001);
    let cents = val % 100;
    val = Math.floor(val / 100).toString();
    if (cents < 10) {
        cents = "0" + cents
    }
    for (var i = 0; i < Math.floor((val.length - (1 + i)) / 3); i++) {
        val = val.substring(0, val.length - (4 * i + 3)) + ',' + val.substring(val.length - (4 * i + 3));
    }
    return (((sign) ? '' : '-') + val + '.' + cents);
});

Vue.filter('toChnum', function (val) {
    if (!val) return ''
    let chin_list = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];//所有的数值对应的汉字
    let sn = parseInt(val);
    return chin_list[sn - 1];
});

Vue.filter('mobil', function (val) {
    if (!val) return ''
    return val.slice(0, 3) + '****' + val.slice(7)
});

Vue.prototype.baseurl = "http://shanzhuan.autocloudchain.com/";

Vue.prototype.errorImage = (e) => {
    e.target.src = require('./assets/img/error.png')
}

//获取地址栏参数
Vue.prototype.getUrlKey = (name) => {
    // return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [""])[1].replace(/\+/g, '%20')) || null
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [""])[1].replace(/\+/g, '%20')) || null;
}

//判断是否为微信浏览器
Vue.prototype.isWeiXin = () => {
    var ua = window.navigator.userAgent.toLowerCase();
    //通过正则表达式匹配ua中是否含有MicroMessenger字符串
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    } else {
        return false;
    }
}

//判断是否在微信小程序里面
let wx
Vue.prototype.isWeiXinapp = (callback) => {
    let ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        //ios的ua中无miniProgram，但都有MicroMessenger（表示是微信浏览器）
        wx.miniProgram.getEnv((res) => {
            callback(res.miniprogram)
        })
    } else {
        callback(false)
    }
}

//封装兼容小程序的路由跳转
Vue.prototype.navigate = (that, data) => {
    that.isWeiXinapp((res) => {
        if (res) {
            let parms = ''
            if (data.query) {
                parms += '?';
                for (let item in data.query) {
                    parms += ('&' + item + '=' + data.query[item])
                }
            }
            let viewUrl = window.location.protocol + "//" + window.location.hostname + data.path + parms;
            wx.miniProgram.navigateTo({ url: '../webview/index?weburl=' + viewUrl });
        } else {
            that.$router.push(data);
        }
    });
}

//计算rem
Vue.prototype.rem = () => {
    let deviceWidth = document.documentElement.clientWidth || window.innerWidth;
    return deviceWidth / 7.5
}
//检测是否登录
Vue.prototype.iflogin = () => {
    if (localStorage.getItem('userinfo')) {
        return true
    } else {
        return false
    }
}

import FastClick from 'fastclick'
//控制引入FastClick后input点击迟钝问题
FastClick.prototype.focus = function (targetElement) {
    var length;
    if (targetElement.setSelectionRange && targetElement.type.indexOf('date') !== 0 && targetElement.type !== 'time' && targetElement.type !== 'month') {
        length = targetElement.value.length;
        targetElement.focus();
        targetElement.setSelectionRange(length, length);
    }
};
FastClick.attach(document.body);
//复制插件
import VueClipboard from 'vue-clipboard2'

Vue.use(VueClipboard)

new Vue({
    router,
    render: h => h(App)
}).$mount('#app')
